﻿using System.Text.Json;
using dotnet.entity;
using dotnet.service;
using Microsoft.AspNetCore.Mvc;

namespace dotnet.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController(UserService userService) : ControllerBase
    {

        private readonly UserService _userService = userService;

        [HttpPost("save")]
        public int SaveUser([FromBody] User user)
        {
            User createUser = new()
            {
                Name = user.Name,
                Age = user.Age,
            };
           int result = _userService.SaveUser(createUser.Name, createUser.Age);
           return result; 
        }



        [HttpPost("query")]
        public List<User> GetUsersPaged([FromBody] UserQuery userQuery)

        {
            int pageNumber = userQuery.PageNumber;
            int pageSize = userQuery.PageSize;
               
            if (pageNumber < 1) throw new ArgumentOutOfRangeException(JsonSerializer.Serialize(userQuery), "pageNumber number must be greater than 0.");
            if (pageSize < 1) throw new ArgumentOutOfRangeException(JsonSerializer.Serialize(userQuery), "pageSize must be greater than 0.");

            List<User> userList = _userService.GetUsersPaged(pageNumber, pageSize);

            return userList;
        }
    }
}
